package com.xiaoq.order.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xiaoq.apis.interpreter.IInterpreterApi;
import com.xiaoq.apis.order.IOrderApi;
import com.xiaoq.order.entity.PayState;
import com.xiaoq.order.entity.ShopOrderEntity;
import com.xiaoq.order.service.IShopOrderService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenjh on 2017/12/22.
 */
@Service(interfaceClass=IOrderApi.class, version="1.0.0")
public class OrderApiImpl implements IOrderApi {

    @Autowired
    private IShopOrderService shopOrderService;

    @Override
    public Map<String, Integer> getInterOrderPaidCount(String shopId, List<String> interIdList) {
        Map<String, Integer> resultMap = new HashMap<>();
        if(StringUtils.isBlank(shopId) || CollectionUtils.isEmpty(interIdList)){
            return resultMap;
        }

        List<ShopOrderEntity> shopOrderList = shopOrderService.getInterOrderList(shopId,
                PayState.PAID.value(),interIdList);
        if(CollectionUtils.isEmpty(shopOrderList)){
            return resultMap;
        }

        for(ShopOrderEntity shopOrder : shopOrderList){
            String interpreterId = shopOrder.getInterpreterId();
            if(StringUtils.isNotBlank(interpreterId)){
                if(resultMap.containsKey(interpreterId)){
                    resultMap.put(interpreterId, resultMap.get(interpreterId) + 1);
                }else {
                    resultMap.put(interpreterId, 1);
                }
            }
        }
        return resultMap;
    }
}
